package com.aijk.web.admin.controller.sys;

import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.OptContentApi;
import com.aijk.ehm.model.OptContent;
import com.aijk.ehm.model.qo.OptContentQo;
import com.aijk.ehm.sys.model.SysAdmin;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 协议管理
 */
@Controller
@RequestMapping(value = "/admin/optContent")
public class OptContentController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(OptContentController.class);

    @Autowired
    private OptContentApi optContentApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "opt/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        draw += 1;
        OptContentQo qo = DataTableUtil.fillQo(skey, OptContentQo.class);
        PageInfo<OptContent> pageInfo = optContentApi.page(qo);
        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", pageInfo.getList());
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        OptContent bean = new OptContent();
        if (id != null){
            	bean = optContentApi.findOne(id);
        }
        model.addAttribute("mhInfoSrc", bean);
        return "opt/edit";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(OptContent bean) {
        try {
            	Long id = bean.getId();
            SysAdmin admin = getCurrentUser();
            	if (id == null) {// 添加
                    bean.setCreater(admin.getAdminId());
                    optContentApi.save(bean);
            	} else {
                    bean.setCreater(admin.getAdminId());
                    optContentApi.update(bean);
                	}
            }catch (ErrorCodeException e){
                	e.printStackTrace();
                	logger.error(e.getMessage());
                	return RetMsg.failed();
            }
            return RetMsg.success();
        }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg del(@RequestParam(value = "id") Long id) {
        try {
            optContentApi.deleteById(id);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

}